﻿using System;
using System.Collections.Generic;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Windows.Extensions;
using XCI.Helper;
using XCI.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统文件服务
    /// </summary>
    public class FileService : BaseService<SysFile>
    {
        public static readonly FileService Instance = new FileService();
        protected override string RootUrl => "/api/sys/file/";

        /// <summary>
        /// 根据记录主键检查文件是否存在
        /// </summary>
        /// <param name="recordId">记录主键</param>
        public bool ExistByRecordId(string recordId)
        {
            var url = GetUrl("existByRecordId", recordId);
            return SysRuntime.Api.PostAsMessage(url).EnsureSuccess().Success;
        }

        /// <summary>
        /// 新建文件
        /// </summary>
        /// <param name="model">文件模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysFile model)
        {
            if (model.Id.IsEmpty())
            {
                model.Id = NewId();
            }
            return InsertCore(model);
        }

        /// <summary>
        /// 新建文件
        /// </summary>
        /// <param name="fileModel">上传文件模型对象</param>
        /// <param name="recordId">记录主键</param>
        /// <returns>成功返回true</returns>
        public bool Insert(FileModel fileModel, string recordId)
        {
            SysFile model = new SysFile();
            model.Id = NewId();
            model.Name = fileModel.Name;
            model.Url = fileModel.Url;
            model.FileSize = fileModel.Size;
            model.RecordId = recordId;
            model.ContentType = fileModel.ContentType;
            model.Md5 = fileModel.Md5;
            return InsertCore(model);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="id">文件主键</param>
        public bool Delete(string id)
        {
            return DeleteByIdCore(id);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="recordId">记录主键</param>
        public bool DeleteByRecordId(string recordId)
        {
            var url = GetUrl("deleteByRecordId");
            var map = Map.New.Add("recordId", recordId);
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 根据主键获取文件对象
        /// </summary>
        /// <param name="id">文件主键</param>
        public SysFile SelectById(string id)
        {
            return SelectByIdCore(id);
        }

        /// <summary>
        /// 根据记录主键获取第一个文件对象
        /// </summary>
        /// <param name="recordId">记录主键</param>
        public SysFile SelectByRecordId(string recordId)
        {
            var url = GetUrl("selectByRecordId", recordId);
            return SysRuntime.Api.Post<SysFile>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询文件分页列表
        /// </summary>
        /// <param name="paged">分页参数</param>
        /// <param name="recordId">记录主键</param>
        public PageList<SysFile> SelectPageListByRecordId(PageParam paged, string recordId)
        {
            var url = GetUrl("selectPageList");
            var map = ToPageMap(paged).Add("recordId", recordId);
            return SysRuntime.Api.Post<PageList<SysFile>>(url, map).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询文件列表
        /// </summary>
        /// <param name="recordId">记录主键</param>
        public List<SysFile> SelectListByRecordId(string recordId)
        {
            var url = GetUrl("selectListByRecordId", recordId);
            return SysRuntime.Api.Post<List<SysFile>>(url).EnsureSuccess().Data;
        }
    }
}
